package cn.pupil.springframework.aop.framework.adapter;

import cn.pupil.springframework.aop.MethodBeforeAdvice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * @Description: 方法前置通知拦截器
 * @Author: pupil
 * @Date: 2024/11/21 下午 4:46
 */
public class MethodBeforeAdviceInterceptor implements MethodInterceptor {

    // 方法前置通知
    private MethodBeforeAdvice advice;

    public MethodBeforeAdviceInterceptor() {
    }

    public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
        this.advice = advice;
    }

    /**
     * 获取方法前置通知
     *
     * @return
     */
    public MethodBeforeAdvice getAdvice() {
        return advice;
    }

    /**
     * 设置方法前置通知
     *
     * @param advice
     */
    public void setAdvice(MethodBeforeAdvice advice) {
        this.advice = advice;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        this.advice.before(methodInvocation.getMethod(),methodInvocation.getArguments(),methodInvocation.getThis());
        return methodInvocation.proceed();
    }

}
